package com.cdream.basic.algorithm.leetcode.dp;

import cn.hutool.json.JSONUtil;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Question322 {

    public int coinChange(int[] coins, int amount) {

        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        for (int i = 1; i <= coins.length; i++) {
            int coin = coins[i - 1];
            for (int j = coin; j <= amount; j++) {
                dp[j] = Math.min(dp[j], dp[j - coin]+1);
            }

        }
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }

    public int coinChange2(int[] coins, int amount) {

        int[][] dp = new int[coins.length + 1][amount + 1];
        for (int[] ints : dp) {
            Arrays.fill(ints, amount + 1);
        }
        dp[0][0] = 0;
        for (int i = 1; i <= coins.length; i++) {
            int coin = coins[i - 1];
            for (int j = 0; j <= amount; j++) {
                if (j < coin) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - coin] + 1);
                }
            }

        }
        System.out.println(JSONUtil.toJsonStr(dp));
        return dp[coins.length][amount] == amount + 1 ? -1 : dp[coins.length][amount];
    }

    public static void main(String[] args) {
        System.out.println(new Question322().coinChange(new int[]{1, 2, 5}, 11));
    }
}
